package org.varan.emulator;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class RemoteControlClient {
	
	private static final int DEFAULT_PORT =  42620;
	private static Socket socket = null;
	
	public static final RemoteControlClient INSTANCE 
			= new RemoteControlClient(DEFAULT_PORT);
	
	public void send(Message message) {
		try {
			OutputStream out = socket.getOutputStream();
			out.write(message.getBytes());
			out.flush();
		} catch (UnknownHostException e) {
			e.printStackTrace();
			System.exit(-1);
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(-1);
		} 
	}
	
	public static void main(String[] args) {		
		INSTANCE.send(new Message("Hello World!"));
		INSTANCE.close();
	}
	
	private RemoteControlClient(int port) {
		try {
			socket = new Socket("localhost", port);
		} catch (UnknownHostException e) {
			socket = null;
			e.printStackTrace();
		} catch (IOException e) {
			socket = null;
			e.printStackTrace();
		}
	}
	
	public void close() {
		try {
			if (socket != null) {
				socket.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
